package com.tengniu.web.controller.buss;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tengniu.buss.domain.Banner;
import com.tengniu.buss.service.BannerService;
import com.tengniu.common.annotation.Log;
import com.tengniu.common.buss.enums.YesOrNoEnum;
import com.tengniu.common.core.controller.BaseController;
import com.tengniu.common.core.domain.AjaxResult;
import com.tengniu.common.core.page.TableDataInfo;
import com.tengniu.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 轮播图管理
 */
@RestController
@RequestMapping("/admin/banner")
@Slf4j
public class BannerAdminController extends BaseController {

    @Resource
    private BannerService bannerService;

    /**
     * 获取轮播图
     */
    @PreAuthorize("@ss.hasPermi('buss:banner:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Banner banner) {
        startPage();
        List<Banner> list = bannerService.list(
                Wrappers.lambdaQuery(Banner.class)
                        .eq(Objects.nonNull(banner.getType()), Banner::getType, banner.getType())
                        .like(Objects.nonNull(banner.getTitle()), Banner::getTitle, banner.getTitle()));
        return getDataTable(list);
    }

    /**
     * 新增轮播图
     */
    @PreAuthorize("@ss.hasPermi('buss:banner:add')")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Banner banner) {
        return toAjax(bannerService.save(banner));
    }

    /**
     * 修改轮播图
     */
    @PreAuthorize("@ss.hasPermi('buss:banner:edit')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Banner banner) {
        return toAjax(bannerService.updateById(banner));
    }

    /**
     * 删除轮播图
     */
    @PreAuthorize("@ss.hasPermi('buss:banner:remove')")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{bannerId}")
    public AjaxResult remove(@PathVariable Long bannerId) {
        return toAjax(bannerService.removeById(bannerId));
    }

    /**
     * 上架
     */
    @PreAuthorize("@ss.hasPermi('buss:banner:shelf')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @GetMapping("/shelf/{bannerId}")
    public AjaxResult shelf(@PathVariable Long bannerId) {
        Banner banner = bannerService.getById(bannerId);
        if (banner == null) {
            return AjaxResult.error("轮播图不存在");
        }
        banner.setStatus(YesOrNoEnum.YES.getCode());
        return toAjax(bannerService.updateById(banner));
    }

    /**
     * 下架
     */
    @PreAuthorize("@ss.hasPermi('buss:banner:offShelf')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @GetMapping("/offShelf/{bannerId}")
    public AjaxResult offShelf(@PathVariable Long bannerId) {
        Banner banner = bannerService.getById(bannerId);
        if (banner == null) {
            return AjaxResult.error("轮播图不存在");
        }
        banner.setStatus(YesOrNoEnum.NO.getCode());
        return toAjax(bannerService.updateById(banner));
    }

}
